package com.example.springresource02.demo;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @author hu wenhao
 * @version 1.0.0
 * @description
 * @date 2022/6/6 10:13
 */
public class TestApplicationContext {

    public static void main(String[] args) {
//        testClassPathXmlApplicationContext();
//        testFileSystemXmlApplicationContext();
        testAnnotationConfigApplicationContext();

    }

    public static void testReader(){
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        System.out.println("读取之前。。。。。");

        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        System.out.println("读取之后。。。。。");
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(new ClassPathResource("spring-beans.xml"));

        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
    }

    
    /** 
     * @description: 基于classpath下xml的配置文件创建
     * @author hu wenhao
     * @date: 2022/6/6 16:18
     */ 
    
    private static void testClassPathXmlApplicationContext(){
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext("spring-beans.xml");

        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println("beanDefinitionName = " + beanDefinitionName);
        }

    }

    /** 
     * @description:基于磁盘路径下 xml配置文件创建
     * @author hu wenhao
     * @date: 2022/6/6 16:18
     */ 
    
    private static void testFileSystemXmlApplicationContext(){
        FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("C:\\Users\\Andriod\\IdeaProjects\\mini-spring\\spring-resource-02\\src\\main\\resources\\spring-beans.xml");

        for (String beanDefinitionName : fileSystemXmlApplicationContext.getBeanDefinitionNames()) {
            System.out.println("beanDefinitionName = " + beanDefinitionName);
        }
    }

    /** 
     * @description: 基于java配置类来创建
     * @author hu wenhao
     * @date: 2022/6/6 16:18
     */ 
    
    private static void testAnnotationConfigApplicationContext(){
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(TestBeanFactory.Config.class);
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
    }

    /** 
     * @description: 和基于java配置类创建，用于web环境
     * @author hu wenhao
     * @date: 2022/6/6 16:18
     */ 
    
    private static void testAnnotationConfigServletWebServerApplicationContext(){
        AnnotationConfigServletWebServerApplicationContext context
                = new AnnotationConfigServletWebServerApplicationContext();
    }

}
